package yo.latyoo.hokage.zetsu.executor;

import yo.latyoo.hokage.tools.consts.LogConst;
import yo.latyoo.hokage.zetsu.handler.ZetsuHandler;
import yo.latyoo.hokage.zetsu.handler.annotation.ZetsuJob;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZetsuExecutor {
    public  String appName;
    public String skmrAddress;
    public String myAddress;
    public String logPath;
    public int logRetentionDays;
    public String accessToken;

    public void start() {
        //存储调度中心地址
        putHokage(hokageAddress,accessToken);
        //将服务注册到hokage
        RegistryHokageThread.INSTANCE().start(appName,executorAddress);
        //启动回调线程，当任务执行完毕进行回调
        CallbackThread.INSTANCE().start();
    }


    /**************************************** start 注册使用了Zetsu Job Handler注解的方法****************************************************/
    private static final Map<String, ZetsuHandler> ZETSU_JOB_HANDLER_MAP = new ConcurrentHashMap<>();

    public static ZetsuHandler getJobHandler(String name) {
        return ZETSU_JOB_HANDLER_MAP.get(name);
    }

    public void putJobMethodHandler(ZetsuJob zetsuJob, Object bean, Method executeMethod) {

        String name = zetsuJob.value();
        Class<?> clazz = bean.getClass();
        String methodName = executeMethod.getName();
        if (name.trim().length() == 0) {
            throw new RuntimeException(LogConst.PREFIX_ZETSU+"method name invalid, for[" + clazz + "#" + methodName + "] .");
        }
        if (getJobHandler(name) != null) {
            throw new RuntimeException(LogConst.PREFIX_ZETSU+"method[" + name + "] naming conflicts.");
        }
        executeMethod.setAccessible(true);
        ZETSU_JOB_HANDLER_MAP.put(name, new ZetsuHandler(bean, executeMethod));
    }
    /**************************************** end 注册使用了Zetsu Job Handler注解的方法****************************************************/

}
